package top.hcode.hoj.service.admin.tag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.common.exception.StatusFailException;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.dao.problem.TagDao;
import top.hcode.hoj.manager.admin.tag.AdminTagManager;
import top.hcode.hoj.pojo.dto.SaveTagRelDTO;
import top.hcode.hoj.pojo.entity.problem.Tag;
import top.hcode.hoj.pojo.entity.problem.TagClassification;

import java.util.List;

/**
 * @Author: Himit_ZH
 * @Date: 2022/3/9 17:46
 * @Description:
 */
@Service
public class AdminTagService {

    @Autowired
    private AdminTagManager adminTagManager;

    @Autowired
    private TagDao tagDao;

    @Autowired
    private LazyDao lazyDao;

    public List<Tag> getTagList(Long tcid) {
        QueryWrapper<Tag> query = Wrappers.query();
        if (tcid != null) {
            query.eq("tcid", tcid);
        }
        query.orderByAsc("tcid", "id");
        return tagDao.list(query);
    }

    public CommonResult<Tag> addTag(Tag tag) {
        try {
            return CommonResult.successResponse(adminTagManager.addTag(tag));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    
    public CommonResult<Void> updateTag(Tag tag) {
        try {
            adminTagManager.updateTag(tag);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    
    public CommonResult<Void> deleteTag(Long tid) {
        try {
            adminTagManager.deleteTag(tid);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    
    public CommonResult<List<TagClassification>> getTagClassification(String oj) {
        return CommonResult.successResponse(adminTagManager.getTagClassification(oj));
    }

    
    public CommonResult<TagClassification> addTagClassification(TagClassification tagClassification) {
        try {
            return CommonResult.successResponse(adminTagManager.addTagClassification(tagClassification));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    
    public CommonResult<Void> updateTagClassification(TagClassification tagClassification) {
        try {
            adminTagManager.updateTagClassification(tagClassification);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    
    public CommonResult<Void> deleteTagClassification(Long tcid) {
        try {
            adminTagManager.deleteTagClassification(tcid);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Transactional
    public String saveTagRel(SaveTagRelDTO saveTagRelDTO) {
        lazyDao.runSql("delete from tag_rel where target_id = ? and target_type = ?", saveTagRelDTO.getTargetId(), saveTagRelDTO.getTargetType());
        for (Long tagId : saveTagRelDTO.getTagIds()) {
            lazyDao.runSql("insert into tag_rel (tag_id, target_id, target_type) values (?, ?, ?)", tagId, saveTagRelDTO.getTargetId(), saveTagRelDTO.getTargetType());
        }
        return null;
    }
}
